package com.stu;

import org.junit.jupiter.api.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @Classname CompletablreFutureDemo
 * @Version 1.0.0
 * @Date 2022/2/21 9:28
 * @Created by yd
 */
public class CompletablreFutureDemo {
    @Test
    public void allOf() {
        CompletableFuture<Void> fu = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("CompletablreFutureDemo.allOf====");
        });
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {

            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("CompletablreFutureDemo.allOf----------");
        });

        System.out.println("阻塞");
        //这里用 join 和get 都行，(get会抛出异常等)
        CompletableFuture.allOf(future, fu).join();
        System.out.println("end");
    }
}
